package com.wholesale.utils;

import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.IBinder;

public class UpdateUIService extends Service {
	CommandReceiver cmdReceiver;
	boolean flag;

	boolean isOpenCar = false;

	@Override
	public void onCreate() {// 重写onCreate方法
		flag = true;
		cmdReceiver = new CommandReceiver();
		IntentFilter filter = new IntentFilter();// 创建IntentFilter对象
		filter.addAction("com.wholesale.utils.UpdateUIService");
		registerReceiver(cmdReceiver, filter);// 注册Broadcast Receiver
		super.onCreate();
	}

	@Override
	public IBinder onBind(Intent intent) {// 重写onBind方法
		// TODO Auto-generated method stub
		return null;
	}
	// 方法：
	public void doJob() {
		new Thread() {
			public void run() {
				// while (flag) {
				// try {// 睡眠一段时间
				// Thread.sleep(1000);
				// } catch (Exception e) {
				// e.printStackTrace();
				// }
				Intent intent = new Intent();// 创建Intent对象
				intent.setAction("com.wholesale.WholeSale");
				intent.putExtra("cmd", Constants.CMD_ADDCAR_SERVICE);
				sendBroadcast(intent);// 发送广播
				if (isOpenCar) {
					intent.setAction("com.wholesale.CartActivity");
					intent.putExtra("cmd", Constants.CMD_ADDCAR_SERVICE);
					sendBroadcast(intent);// 发送广播
				}
				// }
			}
		}.start();
	}

	private class CommandReceiver extends BroadcastReceiver {// 继承自BroadcastReceiver的子类
		@Override
		public void onReceive(Context context, Intent intent) {
			// TODO Auto-generated method stub
			int cmd = intent.getIntExtra("cmd", -1);// 获取Extra信息
			if (cmd == Constants.CMD_STOP_SERVICE) {// 如果发来的消息是停止服务
				stopSelf();// 停止服务
			} else if (cmd == Constants.CMD_OPENCAR_SERVICE) {
				isOpenCar = true;
			} else if (cmd == Constants.CMD_ADDCAR_SERVICE) {
				Intent addintent = new Intent();// 创建Intent对象
				addintent.setAction("com.wholesale.WholeSale");
				addintent.putExtra("cmd", Constants.CMD_ADDCAR_SERVICE);
				sendBroadcast(addintent);// 发送广播
				if (isOpenCar) {
					Intent updateIntent = new Intent();// 创建Intent对象
					updateIntent.setAction("com.wholesale.CartActivity");
					updateIntent.putExtra("cmd",
							Constants.CMD_UPDATECAR_SERVICE);
					sendBroadcast(updateIntent);// 发送广播
				}
			} else if (cmd == Constants.CMD_DELETECAR_SERVICE) {
				Intent myintent = new Intent();// 创建Intent对象
				myintent.setAction("com.wholesale.WholeSale");
				myintent.putExtra("cmd", Constants.CMD_DELETECAR_SERVICE);
				sendBroadcast(myintent);// 发送广播
			} else if (cmd == Constants.CMD_UPDATECAR_SERVICE) {
				Intent myintent = new Intent();// 创建Intent对象
				myintent.setAction("com.wholesale.WholeSale");
				myintent.putExtra("cmd", Constants.CMD_UPDATECAR_SERVICE);
				sendBroadcast(myintent);// 发送广播
				if (isOpenCar) {
					Intent updateIntent = new Intent();// 创建Intent对象
					updateIntent.setAction("com.wholesale.CartActivity");
					updateIntent.putExtra("cmd",
							Constants.CMD_UPDATECAR_SERVICE);
					sendBroadcast(updateIntent);// 发送广播
				}
			} else if (cmd == Constants.CMD_LOGIN_SERVICE) {
				Intent myintent = new Intent();// 创建Intent对象
				myintent.setAction("com.wholesale.WholeSaleActivity");
				myintent.putExtra("cmd", cmd);
				sendBroadcast(myintent);// 发送广播
			} else if (cmd == Constants.CMD_ADDFAVORITE_SERVICE) {
				if (Constants.initMoreActivity) {
					Intent myintent = new Intent();// 创建Intent对象
					myintent.setAction("com.wholesale.MoreActivity");
					myintent.putExtra("cmd", cmd);
					sendBroadcast(myintent);// 发送广播
				}
			} else if (cmd == Constants.CMD_LOGOUT_SERVICE) {
				Intent myintent = new Intent();// 创建Intent对象
				myintent.setAction("com.wholesale.WholeSaleActivity");
				myintent.putExtra("cmd", cmd);
				sendBroadcast(myintent);// 发送广播
				if (isOpenCar) {
					Intent updateIntent = new Intent();// 创建Intent对象
					updateIntent.setAction("com.wholesale.CartActivity");
					updateIntent.putExtra("cmd", Constants.CMD_LOGOUT_SERVICE);
					sendBroadcast(updateIntent);// 发送广播
				}
			} else if (cmd == Constants.CMD_MYACCOUNT_SERVICE) {
				Intent myintent = new Intent();// 创建Intent对象
				myintent.setAction("com.wholesale.WholeSale");
				myintent.putExtra("cmd", cmd);
				sendBroadcast(myintent);// 发送广播
				if (Constants.initMoreActivity) {
					Intent my1intent = new Intent();
					my1intent.setAction("com.wholesale.MoreActivity");
					my1intent.putExtra("cmd", Constants.initMoreActivity);
					sendBroadcast(my1intent);// 发送广播
				}
			}
		}
	}

	@Override
	public void onDestroy() {// 重写onDestroy方法
		this.unregisterReceiver(cmdReceiver);// 取消注册的CommandReceiver
		super.onDestroy();
	}
}
